package com.xr.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.sunnyday.constant.MessageConstant;
import com.xr.sunnyday.entity.PageResult;
import com.xr.sunnyday.entity.QueryPageBean;
import com.xr.sunnyday.entity.Result;
import com.xr.sunnyday.pojo.CheckGroup;
import com.xr.sunnyday.service.CheckgroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ResponseBody
@Controller
@RequestMapping("/Checkgroup")
public class CheckgroupController {
    @Reference
    private CheckgroupService checkgroupService;

    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> list = checkgroupService.findAll();
        if (list != null && list.size() > 0) {
            return new Result(true, "查询成功", list);
        }
        return new Result(false, "查询失败");
    }

    @RequestMapping("/findpage")
    public Result findpage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkgroupService.findPage(queryPageBean);
        return new Result(true , MessageConstant.DELETE_CHECKGROUP_SUCCESS,pageResult);
    }

    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        //Conreoller不做业务处理，将需要的参数都传递给业务层
        checkgroupService.add(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findByid")
    public Result findByid(int checkGroupId){
        System.out.println("findByid");
        CheckGroup checkGroup = checkgroupService.findByid(checkGroupId);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    @GetMapping("/findCheckItemIdBYfindCheckGroupId")
    public Result findCheckItemIdBYfindCheckGroupId(int checkGroupId){
        List<Integer> checkItemIds = checkgroupService.findCheckItemIdBYfindCheckGroupId(checkGroupId);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemIds);
    }

    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup, int[] checkitemIds){
        checkgroupService.update(checkGroup,checkitemIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delete(int checkGroupId){
        checkgroupService.delete(checkGroupId);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
}
